package de.fhwgt.quiz.servlets;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import de.fhwgt.quiz.application.Quiz;
import de.fhwgt.quiz.loader.FilesystemLoader;
import de.fhwgt.quiz.loader.LoaderException;

/**
 * Servlet implementation class CatalogServlet
 */
@WebServlet("/CatalogServlet")
public class CatalogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Quiz oQuiz;
    private Set<String> sKeyset;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CatalogServlet() {
        super();
        //CatalogLoader initalisieren
        FilesystemLoader oCatLoader = new FilesystemLoader("de/fhwgt/quiz/catalogs");
        oQuiz = Quiz.getInstance();
        oQuiz.initCatalogLoader(oCatLoader);
        System.out.println("CatLoader init");
        try {
			sKeyset = oQuiz.getCatalogList().keySet();
		} catch (LoaderException e) {
			System.out.println("error");
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			DocumentBuilderFactory oDocBuildFactory = DocumentBuilderFactory.newInstance();
         	DocumentBuilder oDocBuilder = oDocBuildFactory.newDocumentBuilder();
         	//Root Element erstellen
	        Document oDoc = oDocBuilder.newDocument();
	        Element eRoot = oDoc.createElement("catalogResponse");
         	oDoc.appendChild(eRoot);

         	
         	String selectedCatalog = null;
         	if(oQuiz.getCurrentCatalog() != null)
         	{
         		selectedCatalog = oQuiz.getCurrentCatalog().getName();
         	}
         		
	        for(String sCatName : sKeyset) 
	        {
	        	//Alle Namen der vorhandenen Kataloge in XML schreiben
	        	Element eCatalog = oDoc.createElement("catalog");
	        	eCatalog.appendChild(oDoc.createTextNode(sCatName));

	        	if(sCatName.equals(selectedCatalog))
	        		eCatalog.setAttribute("selected", "true");
	        	else
	        		eCatalog.setAttribute("selected", "false");
	        	 
	        	eRoot.appendChild(eCatalog);
	        }
             ////XML in in response schreiben und verschicken
             TransformerFactory transformerFactory = TransformerFactory.newInstance();
             Transformer transformer = transformerFactory.newTransformer();                                  
             DOMSource oSource = new DOMSource(oDoc);
             StreamResult oOutput = new StreamResult(response.getWriter());
             transformer.transform(oSource, oOutput);
			 
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		} catch (TransformerException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}    
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
